package app.cloudstringers;

import utils.Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import controller.CompletedAsync;
import define.Constants;
import define.DefaultValue;
import define.SharedPreference;

public class Completed extends Activity {
	/**
	 * String section
	 */
	private String ID = null;

	/**
	 * Data section
	 */

	/**
	 * View section
	 */
	private AlertDialog.Builder mAdLogout;
	private AlertDialog.Builder mAdExit, mAdConnection;

	/**
	 * The Other Tools
	 */
	private SharedPreferences.Editor mSpEditor;
	private SharedPreferences mSpSettings;

	/**
	 * Initialize decoder for streaming & playing video
	 */

	/**
	 * TODO BEGIN LOAD THE LIBRARY
	 */
	static {
		try {
			System.loadLibrary("ffmpeg");
		} catch (UnsatisfiedLinkError e) {
			Log.i("", "Error : " + e.toString());
		}

	}

	// public native String getString();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_completed);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

		// TODO RUN FROM HERE
		// Log.i("", "" + getString());

		// TODO SKIP THIS

		// Initial alert dialogs
		initialAlertDialogs();

		// Get stored data from shared preferences
		mSpSettings = getSharedPreferences(SharedPreference.PREFS,
				Context.MODE_PRIVATE);
		mSpEditor = mSpSettings.edit();
	}

	/**
	 * On Clicked
	 */
	public void onClickCompleted(View v) {
		switch (v.getId()) {
		case R.id.ibtn_home_in_completed:
			// new Intent to menu class
			Intent intent = new Intent(this, Home.class);
			finish();
			startActivity(intent);
			break;
		case R.id.ibtn_logout_in_completed:
			mAdLogout.show();
			break;
		case R.id.ibtn_refresh_in_completed:
			new CompletedAsync(Completed.this).execute(ID);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			mAdExit.show();
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();

		if (Constants.utils.isNetworkOnline()) {
			// Get ID
			ID = mSpSettings.getString(SharedPreference.PREFS_KEY_GLOBAL_ID,
					DefaultValue.DEFAULT_GLOBAL_ID + "");

			new CompletedAsync(Completed.this).execute(ID);
		} else {
			Utils.hideConnectionDialog();
			Utils.showConnectionDialog();
		}
	}

	@Override
	public void onStop() {
		super.onStop();

		System.gc();
	}

	/**
	 * Initial methods
	 */
	private void initialAlertDialogs() {
		mAdConnection = new AlertDialog.Builder(this);
		mAdConnection.setTitle(getString(R.string.title_failed_connection));
		mAdConnection.setIcon(R.drawable.ibtn_error_normal);
		mAdConnection.setCancelable(false);
		mAdConnection
				.setMessage(getString(R.string.content_failed_server_connection));
		mAdConnection.setPositiveButton(R.string.try_again,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
					}
				});
		mAdConnection.setNegativeButton(R.string.quit,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});

		mAdLogout = new AlertDialog.Builder(this);
		mAdLogout.setTitle(getString(R.string.title_logout));
		mAdLogout.setCancelable(false);
		mAdLogout.setMessage(getString(R.string.content_logout));
		mAdLogout.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						// set data for shared preferences
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_USER_NAME, "user");
						mSpEditor.putString(
								SharedPreference.PREFS_KEY_GLOBAL_ID,
								DefaultValue.DEFAULT_GLOBAL_ID + "");
						mSpEditor.commit();

						// new intent to login page
						Intent intent = new Intent(Completed.this, Login.class);
						finish();
						startActivity(intent);
					}
				});
		mAdLogout.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});

		mAdExit = new AlertDialog.Builder(Completed.this);
		mAdExit.setTitle(R.string.title_exit);
		mAdExit.setIcon(R.drawable.ic_info_normal);
		mAdExit.setCancelable(false);
		mAdExit.setMessage(R.string.content_exit);
		mAdExit.setPositiveButton(R.string.ok,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});
		mAdExit.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog9, int whichButton) {
					}
				});
	}
}
